package com.ipan.weixin.msg.general;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.apache.http.util.Args;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 接收普通消息
 * 
 * @author iPan
 * @version 2013-12-23
 */
public abstract class AbstractXmlMsg implements XmlMsg, Serializable {
	
	/** serialVersionUID */
	private static final long serialVersionUID = 7908348686894099627L;
	
	/** 开发者微信号（必须） */
	protected String toUserName;
	
	/** 发送方帐户（一个OpenID）（必须） */
	protected String fromUserName;
	
	/** 消息创建时间 秒（整型）（必须） */
	protected String createTime;
	
	/** 消息类型（必须） */
	protected String msgType;
	
	/** 传输类型 */
	protected TransportType transportType;
	
	public AbstractXmlMsg() {}
	
	public AbstractXmlMsg(TransportType transportType) {
		this.transportType = transportType;
	}
	
	public AbstractXmlMsg(String toUserName, String fromUserName, String createTime, String msgType, TransportType transportType) {
		this.toUserName = toUserName;
		this.fromUserName = fromUserName;
		this.createTime = createTime;
		this.msgType = msgType;
		this.transportType = transportType;
	}

	public String getToUserName() {
		return toUserName;
	}

	public void setToUserName(String toUserName) {
		this.toUserName = toUserName;
	}

	public String getFromUserName() {
		return fromUserName;
	}

	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getMsgType() {
		return msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	/**
	 * 接收XML消息
	 */
	public void fromXml(Document doc) {
		Element root = doc.getRootElement();
		this.toUserName = root.elementTextTrim(EL_TO_USER_NAME);
		this.fromUserName = root.elementTextTrim(EL_FROM_USER_NAME);
		this.createTime = root.elementTextTrim(EL_CREATE_TIME);
		this.msgType = root.elementTextTrim(EL_MSG_TYPE);
	}
	
	/**
	 * 回复XML消息
	 * 
	 * @param nonEmpty true 只输出不为空的字段；false 输出所有字段；
	 */
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder();
		if (nonEmpty) {
			if (StringUtils.isNotEmpty(this.toUserName)) {
				buf.append(wrapElementHead(EL_TO_USER_NAME)).append(this.toUserName).append(wrapElementEnd(EL_TO_USER_NAME));
			}
			if (StringUtils.isNotEmpty(this.fromUserName)) {
				buf.append(wrapElementHead(EL_FROM_USER_NAME)).append(this.fromUserName).append(wrapElementEnd(EL_FROM_USER_NAME));
			}
			if (StringUtils.isNotEmpty(this.createTime)) {
				buf.append(wrapElementHead(EL_CREATE_TIME)).append(this.createTime).append(wrapElementEnd(EL_CREATE_TIME));
			}
		} else {
			buf.append(wrapElementHead(EL_TO_USER_NAME)).append(getStringValue(this.toUserName)).append(wrapElementEnd(EL_TO_USER_NAME));
			buf.append(wrapElementHead(EL_FROM_USER_NAME)).append(getStringValue(this.fromUserName)).append(wrapElementEnd(EL_FROM_USER_NAME));
			buf.append(wrapElementHead(EL_CREATE_TIME)).append(getStringValue(this.createTime)).append(wrapElementEnd(EL_CREATE_TIME));
		}
		return buf.toString();
	}
	
	public String toXml() {
		return toXml(false);
	}

	public TransportType getTransportType() {
		return transportType;
	}
	
	protected String wrapElementHead(String element) {
		Args.notNull(element, "XML元素不能为空.");
		return "<" + element + ">";
	}
	
	protected String wrapElementEnd(String element) {
		Args.notNull(element, "XML元素不能为空.");
		return "</" + element + ">";
	}
	
	protected String getStringValue(String value) {
		return (value != null) ? value : "";
	}
	
}
